<template>
  <el-dialog
    :title="title"
    :visible.sync="isShow"
    @close="close"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="(item,index) in peoples" :key="index" :label="item.username" :value="item.username" />
        </el-select>

      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <span />
    <span slot="footer">
      <el-button size="small" @click="isShow= false">取消</el-button>
      <el-button size="small" type="primary" @click="btnOk">确定</el-button>
    </span>
  </el-dialog>

</template>
<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const formDefault = {
      name: '', // 部门名称
      code: '', // 部门编码
      manager: '', // 部门管理者
      introduce: ''// 部门介绍

    }
    const checkNameRepeat = async(rules, value, callback) => {
      const { depts } = await getDepartments()

      let isRepeat = false
      if (this.formData.id) {
        // 有id就是编辑模式
        // 编辑 张三 => 校验规则 除了我之外 同级部门下 不能有叫张三的
        isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        // 没id就是新增模式
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }

      isRepeat ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }

    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 修改判断
        isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
    }
    return {

      isShow: false,
      // 表单数据
      formData: {
        ...formDefault
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      peoples: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.peoples = res
    },
    async btnOk() {
      try {
        await this.$refs.deptForm.validate()
      } catch (error) {
        console.log(error)
        return this.$message.error('校验失败')
      }
      if (this.formData.id) {
        // 修改
        await updateDepartments(this.formData)
      } else {
        this.formData.pid = this.treeNode.id
        await addDepartments(this.formData)
      }

      this.$message.success('操作成功！')

      this.isShow = false
      this.$emit('addDepts')
    },
    async getDepartDetail(id) {
      const res = await getDepartDetail(id)

      this.formData = res
    },
    close() {
      this.formData = { ...this.formDefault }
    }
  }
}

</script>
